package com.lz.gmall.web.controller;

import com.lz.gamll.feign.item.ItemFeignClient;
import com.lz.gmall.product.vo.SkuDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ItemController {

    @Autowired
    private ItemFeignClient itemFeignClient;

    @GetMapping("/{skuId}.html")
    public String item(@PathVariable("skuId") Long skuId, Model model){

        SkuDetailVo skuDetailVo = itemFeignClient.getSkuDetailBySkuId(skuId).getData();

        //skuDetailVo由于调用过程中出现问题，可能出现null，下面出现空指针导致全局异常处理器捕获返回给前端，不太友好
        if (skuDetailVo==null){
            return "item/error";
        }

        // 把查询到的商品详情数据存储到Model数据模型中
        model.addAttribute("categoryView" , skuDetailVo.getCategoryView()) ;
        model.addAttribute("skuInfo" , skuDetailVo.getSkuInfo()) ;
        model.addAttribute("price" , skuDetailVo.getPrice()) ;
        model.addAttribute("spuSaleAttrList" , skuDetailVo.getSpuSaleAttrList()) ;
        model.addAttribute("valuesSkuJson" , skuDetailVo.getValuesSkuJson()) ;

        return "item/index" ;
    }
}
